"""
学员模型
"""
from sqlalchemy import Column, String, Integer, Enum, ForeignKey, DateTime, func
from sqlalchemy.orm import relationship
from .base import Base


class Student(Base):
    """学员表"""
    __tablename__ = "students"
    
    name = Column(String(50), nullable=False, comment="学员姓名")
    gender = Column(Enum("男", "女", name="gender"), nullable=False, comment="性别")
    phone = Column(String(20), unique=True, nullable=False, comment="手机号")
    email = Column(String(100), comment="邮箱")
    region_id = Column(Integer, ForeignKey("regions.id"), comment="地区ID")
    traffic_channel_id = Column(Integer, ForeignKey("traffic_channels.id"), comment="流量渠道ID")
    source = Column(String(50), comment="来源")
    province = Column(String(50), comment="省份")
    status = Column(Enum("potential", "enrolled", "graduated", "dropped", name="student_status"), 
                   default="potential", comment="学员状态")
    
    # 关系
    region = relationship("Region", back_populates="students")
    traffic_channel = relationship("TrafficChannel", back_populates="students")
    follow_ups = relationship("StudentFollowUp", back_populates="student")
    orders = relationship("Order", back_populates="student")
    course_assignments = relationship("StudentCourseAssignment", back_populates="student")
    student_progress = relationship("StudentProgress", back_populates="student")
    course_schedules = relationship("StudentCourseSchedule", back_populates="student")
    
    def __repr__(self):
        return f"<Student(id={self.id}, name='{self.name}', phone='{self.phone}')>"


class StudentFollowUp(Base):
    """学员跟进记录表"""
    __tablename__ = "student_follow_ups"
    
    student_id = Column(Integer, ForeignKey("students.id"), nullable=False, comment="学员ID")
    employee_id = Column(Integer, ForeignKey("employees.id"), nullable=False, comment="跟进员工ID")
    follow_up_type = Column(Enum("call", "wechat", "email", "visit", "other", name="follow_up_type"), 
                           nullable=False, comment="跟进方式")
    follow_up_content = Column(String(1000), comment="跟进内容")
    follow_up_result = Column(Enum("interested", "not_interested", "considering", "scheduled", "no_answer", name="follow_up_result"), 
                             comment="跟进结果")
    next_follow_up_date = Column(DateTime, comment="下次跟进日期")
    follow_up_date = Column(DateTime, nullable=False, comment="跟进时间")
    
    # 关系
    student = relationship("Student", back_populates="follow_ups")
    employee = relationship("Employee", back_populates="follow_ups")
    
    def __repr__(self):
        return f"<StudentFollowUp(id={self.id}, student_id={self.student_id}, employee_id={self.employee_id})>"
